package com.shd.edanpei.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.util.contact.ContactBean;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.contract.UserDetailsContract;
import com.shd.edanpei.presenter.UserDetailsPresenter;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mc on 2018/9/3.
 * 通讯录用户详情
 */

public class UserDetailsActivity extends BaseActivity<UserDetailsPresenter>
        implements UserDetailsContract.UserDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_gender)
    TextView tvGender;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected int setLayout() {
        return R.layout.activity_userdetails;
    }

    @Override
    protected UserDetailsPresenter bindPresenter() {
        return new UserDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("通讯录");

        ContactBean contactBean = getIntent().getParcelableExtra(Constant.DATA);
        setDetails(contactBean);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @OnClick(R.id.tv_phone)
    void clickPhone() {
        final String phone = tvPhone.getText().toString();
        if (!TextUtils.isEmpty(phone)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("拨号确认")
                    .setMessage("确定拨打电话：" + phone)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("拨号", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                    startActivity(intent);
                }
            }).show();
        }
    }

    @Override
    public void setDetails(ContactBean bean) {
        if (!TextUtils.isEmpty(bean.getPicUrl())) {
            loadImageCtrl.getGlide().load(bean.getPicUrl()).into(ivHead);
        }
        tvName.setText(bean.getName());
        tvGender.setText(bean.getGender());
        tvPhone.setText(bean.getMobile());
    }
}
